package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.GoodsCategory;
import com.ruoyi.system.domain.SysGoods;
import com.ruoyi.system.domain.SysStore;
import com.ruoyi.system.mapper.GoodsCategoryMapper;
import com.ruoyi.system.mapper.SysGoodsMapper;
import com.ruoyi.system.mapper.SysStoreMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysStoreStock;
import com.ruoyi.system.service.ISysStoreStockService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品库存信息Controller
 * 
 * @author ruoyi
 * @date 2025-04-21
 */
@Api("商品库存信息")
@RestController
@RequestMapping("/system/storeStock")
public class SysStoreStockController extends BaseController
{
    @Autowired
    private ISysStoreStockService sysStoreStockService;
    @Autowired
    private SysGoodsMapper sysGoodsMapper;
    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;
    @Autowired
    private SysStoreMapper sysStoreMapper;

    /**
     * 查询商品库存信息列表
     */
    // @PreAuthorize("@ss.hasPermi('system:storeStock:list')")
    @GetMapping("/list")
    @ApiOperation("查询商品库存信息列表")
    public TableDataInfo list(SysStoreStock sysStoreStock)
    {
        startPage();
        List<SysStoreStock> list = sysStoreStockService.selectSysStoreStockList(sysStoreStock);
        for (SysStoreStock storeStock : list) {
            // 商品信息
            SysGoods sysGoods = sysGoodsMapper.selectSysGoodsById(storeStock.getGoodsId());
            storeStock.setGoodsName(sysGoods.getGoodsName());
            storeStock.setGoodsPhoto(sysGoods.getGoodsPhoto());
            //分类信息
            GoodsCategory goodsCategory = goodsCategoryMapper.selectGoodsCategoryById(sysGoods.getCategoryId());
            storeStock.setCategoryName(goodsCategory!=null?goodsCategory.getCategoryName():null);
            //门店信息
            SysStore sysStore = sysStoreMapper.selectSysStoreById(storeStock.getStoreId());
            storeStock.setStoreName(sysStore!=null?sysStore.getStoreName():null);
        }
        return getDataTable(list);
    }

    /**
     * 导出商品库存信息列表
     */
    @ApiOperation("导出商品库存信息列表")
    // @PreAuthorize("@ss.hasPermi('system:storeStock:export')")
    @Log(title = "商品库存信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysStoreStock sysStoreStock)
    {
        List<SysStoreStock> list = sysStoreStockService.selectSysStoreStockList(sysStoreStock);
        ExcelUtil<SysStoreStock> util = new ExcelUtil<SysStoreStock>(SysStoreStock.class);
        util.exportExcel(response, list, "商品库存信息数据");
    }

    /**
     * 获取商品库存信息详细信息
     */
    @ApiOperation("获取商品库存信息详细信息")
    // @PreAuthorize("@ss.hasPermi('system:storeStock:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(sysStoreStockService.selectSysStoreStockById(id));
    }

    /**
     * 新增商品库存信息
     */
    @ApiOperation("新增商品库存信息")
    // @PreAuthorize("@ss.hasPermi('system:storeStock:add')")
    @Log(title = "商品库存信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysStoreStock sysStoreStock)
    {
        return toAjax(sysStoreStockService.insertSysStoreStock(sysStoreStock));
    }

    /**
     * 修改商品库存信息
     */
    @ApiOperation("修改商品库存信息")
    // @PreAuthorize("@ss.hasPermi('system:storeStock:edit')")
    @Log(title = "商品库存信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysStoreStock sysStoreStock)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        sysStoreStock.setUpdateBy(user.getUserId() + "");
        if (sysStoreStockService.updateSysStoreStock(sysStoreStock) == -1){
            return AjaxResult.error("库存不足，请重新输入");
        }
        return AjaxResult.success();
    }

    /**
     * 删除商品库存信息
     */
    @ApiOperation("删除商品库存信息")
    // @PreAuthorize("@ss.hasPermi('system:storeStock:remove')")
    @Log(title = "商品库存信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(sysStoreStockService.deleteSysStoreStockByIds(ids));
    }
}
